package com.www.dao.admin;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.www.domain.admin.Menu;

/**
 * 菜单管理dao
 */
@Repository
public interface MenuDao {
	/*
	 * 菜单插入操作
	 */
	@Insert("insert into menu(id,parentId,name,url,icon) values(default,#{parentId},#{name},#{url},#{icon})")
	public int add(Menu menu);
	
	/*
	 * 菜单信息模糊分页搜索查询
	 */
	@Select({"<script>" +
			"select * from menu " +
			"where  1=1 " +
			"<if test='parentId != null'> and parentId like '%${parentId}%'</if>" +
			"<if test='name != null'> and name like '%${name}%'</if>" +
			"<if test='offset != null and pageSize != null'> limit #{offset},#{pageSize}</if>" +
		"</script>"})
	public List<Menu> findList(Map<String, Object> queryMap);
	
	/*
	 * 获取顶级菜单信息
	 */
	@Select("select * from menu where parentId = 0")
	public List<Menu> findTopList();
	
	/*
	 *菜单信息模糊分页搜索查询总记录数 
	 */
	@Select({"<script>" +
			"select count(*) from menu " +
			"where  1=1 " +
			"<if test='parentId != null'> and parentId like '%${parentId}%'</if>" +
			"<if test='name != null'> and name like '%${name}%'</if>" +
		"</script>"})
	public int getTotal(Map<String, Object> queryMap);
	
	/*
	 * 修改菜单信息
	 */
	@Update("update menu set name = #{name},parentId = #{parentId},url = #{url},icon = #{icon} where id = #{id}")
	public int edit(Menu menu);
	
	/*
	 * 删除菜单信息
	 */
	@Delete("delete from menu where id = #{id}")
	public int delete(Long id);
	
	/*
	 * 获取某一分类的子菜单信息
	 */
	@Select("select * from menu where parentId = #{parentId}")
	public List<Menu> findChildernList(Long parentId);
	
	/*
	 * 根据菜单id获取菜单信息
	 */ 
	@Select("select * from menu where id in (select menuId from(select menuId from authority where roleId = #{id})as temp)")
	public List<Menu> findListByIds(Long id);
}
